/* eslint-disable complexity */
/* eslint-disable max-params */
/* eslint-disable max-depth */
/* eslint-disable react-hooks/exhaustive-deps */
import { Button, Checkbox, Form, Input, message } from "antd";
import React, { useEffect, useState } from "react";
import "./index.less";
interface DrawerType {
    isDrawerOpen: boolean;
    spread: any;
    sourceData: any;
    setSourceDataFn: any;
    setisDrawerOpenFn: any;
}
const DrawerContent: React.FC<DrawerType> = ({ isDrawerOpen, spread, sourceData, setSourceDataFn, setisDrawerOpenFn }) => {
    const [form] = Form.useForm();
    const [selections, setSelections] = useState<any>("");
    const [sheetName, setSheetName] = useState<any>("");
    const [tableFlag, setTableFlag] = useState<boolean>(false);
    const [dataFlag, setDataFlag] = useState<boolean>(false);
    const [isDataSource, setIsDataSource] = useState<boolean>(false);
    // eslint-disable-next-line max-params
    // 设置只读
    const sourceDataFn = (sourceData: any, sheetName: any, selections: any, setSourceDataFn: any, value: any) => {
        const field = value.fieldName.split(",");
        let rowNum = Number(field[field.length - 2] ? field[field.length - 2] : "row");
        let colNum = Number(field[field.length - 1] ? field[field.length - 1] : "col");
        if (!isNaN(rowNum) && !isNaN(colNum)) {
            let sourceDatas = JSON.parse(JSON.stringify(sourceData));
            if (sourceDatas) {
                if (sourceDatas[sheetName]) {
                    for (let i = 0; i < selections[0].rowCount; i++) {
                        const row = selections[0].row === -1 ? selections[0].row + i + 1 : selections[0].row + i;
                        for (let g = 0; g < selections[0].colCount; g++) {
                            const col = selections[0].col === -1 ? selections[0].col + g + 1 : selections[0].col + g;
                            if (sourceDatas[sheetName]?.[row]?.[col]) {
                                sourceDatas[sheetName][row][col] = {
                                    ...value,
                                    fieldName: `${field[0]}${rowNum + i}_${colNum + g}`,
                                };
                            } else {
                                if (sourceDatas[sheetName][row]) {
                                    sourceDatas[sheetName][row][col] = {};
                                    sourceDatas[sheetName][row][col] = {
                                        ...value,
                                        fieldName: `${field[0]}${rowNum + i}_${colNum + g}`,
                                    };
                                } else {
                                    sourceDatas[sheetName][row] = {};
                                    sourceDatas[sheetName][row][col] = {
                                        ...value,
                                        fieldName: `${field[0]}${rowNum + i}_${colNum + g}`,
                                    };
                                }
                            }
                        }
                    }
                } else {
                    sourceDatas[sheetName] = {};
                    for (let i = 0; i < selections[0].rowCount; i++) {
                        const row = selections[0].row === -1 ? selections[0].row + i + 1 : selections[0].row + i;
                        sourceDatas[sheetName][row] = {};
                        for (let g = 0; g < selections[0].colCount; g++) {
                            const col = selections[0].col === -1 ? selections[0].col + g + 1 : selections[0].col + g;
                            sourceDatas[sheetName][row][col] = {
                                ...value,
                                fieldName: `${field[0]}${rowNum + i}_${colNum + g}`,
                            };
                        }
                    }
                }
            } else {
                sourceDatas = {};
                sourceDatas[sheetName] = {};
                for (let i = 0; i < selections[0].rowCount; i++) {
                    const row = selections[0].row === -1 ? selections[0].row + i + 1 : selections[0].row + i;
                    sourceDatas[sheetName][row] = {};
                    for (let g = 0; g < selections[0].colCount; g++) {
                        const col = selections[0].col === -1 ? selections[0].col + g + 1 : selections[0].col + g;
                        sourceDatas[sheetName][row][col] = {
                            ...value,
                            fieldName: `${field[0]}${rowNum + i}_${colNum + g}`,
                        };
                    }
                }
            }
            setSourceDataFn(sourceDatas);
            message.success("设置成功");
            setisDrawerOpenFn();
        } else {
            message.error("字段名不规范");
        }
    };
    useEffect(() => {
        if (isDrawerOpen) {
            setIsDataSource(false);
            form.setFieldsValue({
                fieldName: "",
                tableName: "",
                dataType: "",
            });
            let sheet = spread.getActiveSheet();
            const sheetName = sheet.name();
            const selections = sheet.getSelections();
            setSelections(selections);
            setSheetName(sheetName);
            if (sourceData[sheetName]) {
                let tableName = sourceData[sheetName]?.[selections[0].row]?.[selections[0].col]?.tableName;
                let fieldName = sourceData[sheetName]?.[selections[0].row]?.[selections[0].col]?.fieldName;
                let tableFlag = true;
                let dataType = sourceData[sheetName]?.[selections[0].row]?.[selections[0].col]?.dataType;
                let dataFlag = true;
                for (let i = 0; i < selections[0].rowCount; i++) {
                    const row = selections[0].row === -1 ? selections[0].row + i + 1 : selections[0].row + i;
                    for (let g = 0; g < selections[0].colCount; g++) {
                        const col = selections[0].col === -1 ? selections[0].col + g + 1 : selections[0].col + g;
                        const filedObj = sourceData[sheetName]?.[row]?.[col];
                        if (filedObj) {
                            setIsDataSource(true);
                            if (tableName !== filedObj.tableName) {
                                tableFlag = false;
                            }
                            if (dataType !== filedObj.dataType) {
                                dataFlag = false;
                            }
                        } else {
                            tableFlag = false;
                            dataFlag = false;
                        }
                    }
                }
                setDataFlag(dataFlag);
                setTableFlag(tableFlag);
                form.setFieldsValue({
                    fieldName: fieldName,
                });
                if (tableFlag) {
                    form.setFieldsValue({
                        tableName: tableName,
                    });
                } else {
                    form.setFieldsValue({
                        tableName: "",
                    });
                }
                if (dataFlag) {
                    form.setFieldsValue({
                        dataType: dataType,
                    });
                } else {
                    form.setFieldsValue({
                        dataType: "",
                    });
                }
            }
        }
    }, [isDrawerOpen]);
    // 保存
    const onFinish = (value: any) => {
        let sheet = spread.getActiveSheet();
        const sheetName = sheet.name();
        const selections = sheet.getSelections();
        sourceDataFn(sourceData, sheetName, selections, setSourceDataFn, value);
    };
    // 重置
    const resetFn = () => {
        let sourceDatas = JSON.parse(JSON.stringify(sourceData));
        if (sourceDatas?.[sheetName]) {
            let sheet = spread.getActiveSheet();
            const sheetName = sheet.name();
            const selections = sheet.getSelections();
            for (let i = 0; i < selections[0].rowCount; i++) {
                const row = selections[0].row === -1 ? selections[0].row + i + 1 : selections[0].row + i;
                for (let g = 0; g < selections[0].colCount; g++) {
                    const col = selections[0].col === -1 ? selections[0].col + g + 1 : selections[0].col + g;
                    const filedObj = sourceDatas[sheetName]?.[row]?.[col];
                    if (filedObj) {
                        delete sourceDatas[sheetName][row][col];
                        sheet.setValue(row, col, "");
                        form.setFieldsValue({
                            tableName: "",
                            dataType: "",
                            fieldName: "",
                        });
                    }
                }
            }
            setSourceDataFn(sourceDatas);
            setisDrawerOpenFn();
        }
        message.success("解除绑定成功");
    };
    return (
        <div className="drawerContent">
            <div style={{ marginBottom: "20px", padding: "16px 16px 0 24px" }}>
                <span>当前选中范围：</span>
                <span>{selections ? `${sheetName}!$H$${selections[0].row}:$H$${selections[0].col}` : ""}</span>
            </div>
            <Form
                name="basic"
                labelCol={{ span: 6 }}
                wrapperCol={{ span: 18 }}
                form={form}
                // initialValues={{ remember: true }}
                onFinish={onFinish}
                autoComplete="off"
            >
                <Form.Item label="表名" help={!tableFlag ? "选中区域表名信息不一致" : ""} name="tableName" rules={[{ required: true, message: "请输入表名" }]}>
                    <Input />
                </Form.Item>
                <Form.Item help="仅显示选中区域左上角单元格字段名" label="字段名" name="fieldName" rules={[{ required: true, message: "请输入字段名" }]}>
                    <Input />
                </Form.Item>
                <Form.Item help={!dataFlag ? "选中区域数据类型信息不一致" : ""} label="数据类型" name="dataType" rules={[{ required: true, message: "请输入数据类型" }]}>
                    <Input />
                </Form.Item>

                <Form.Item>
                    <Button disabled={!isDataSource} onClick={resetFn}>
                        解除绑定
                    </Button>
                    <Button type="primary" htmlType="submit">
                        确定
                    </Button>
                </Form.Item>
            </Form>
        </div>
    );
};

export default DrawerContent;
